var $ = require("../../utils/Ajax.js");
Page({
  data: {
    PictureUrl: getApp().PictureUrl,
    Id: null,
    Goods: null,
    // 退款原因
    Cause: '请选择',
    // 退款说明
    Explain: '',

    files: [],
    ClickUploader: false,
    ServerFiles: [],

    // 底部所有目录控制
    showShopPopup: false,
    animationData: {},

    RefundsCont: ["拍错/多拍/不想要", "协商一致退款", "缺货", "未按约定时间发货","其他"]
  },

  onLoad: function (options) {
    wx.setNavigationBarTitle({
      title: "退货/退款"
    })
    this.setData({
      Id: JSON.parse(options.Goods).id,
      Goods: JSON.parse(options.Goods)
    })
  },

  // 请选择退款原因
  SelectRefunds: function(){
    var animation = wx.createAnimation({
      duration: 400,
      timingFunction: "linear",
      delay: 0
    })
    this.animation = animation
    animation.translateY(658).step()
    this.setData({
      animationData: animation.export(),
      showShopPopup: true
    })
    setTimeout(() => {
      animation.translateY(0).step()
      this.setData({
        animationData: animation.export()
      })
    }, 200)
  },

  // 选择退款原因
  Cause: function(e){
    var Cause = e.detail.value;
    this.setData({
      Cause: Cause
    })
  },

  // 关闭退款原因
  RefundsBgClose: function (e) {
    var Status = e.currentTarget.dataset.status;
    var animation = wx.createAnimation({
      duration: 200,
      timingFunction: "linear",
      delay: 0
    })
    this.animation = animation
    animation.translateY(658).step()
    this.setData({
      animationData: animation.export(),
    })
    setTimeout(function () {
      animation.translateY(0).step()
      this.setData({
        animationData: animation.export(),
        showShopPopup: false
      })
    }.bind(this), 200)
    if (Status != 1){
      this.setData({
        Cause: '请选择'
      })
    }
  },

  // 填写退款说明
  Explain: function(e){
    var Explain = e.detail.value;
    this.setData({
      Explain: Explain
    })
  },

  // 上传图片
  chooseImage: function (e) {
    var that = this;
    wx.chooseImage({
      count: 1,
      sizeType: ['compressed'], // 可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album'], // 可以指定来源是相册还是相机，默认二者都有
      success: function (res) {
        that.setData({
          ClickUploader: true,
          files: that.data.files.concat(res.tempFilePaths)
        });
        wx.showToast({
          title: '正在上传...',
          icon: 'loading',
          mask: true,
          duration: 10000
        })
        wx.uploadFile({
          url: 'https://sl.sx-sss.com/api/upload',
          filePath: that.data.files[0],
          name: 'file',
          success: function (res) {
            wx.hideToast();
            that.setData({
              ServerFiles: JSON.parse(res.data).d.filename
            })
            wx.showToast({
              title: '上传成功',
              icon: 'success',
              duration: 1500
            });
          },
          fail: function (res) {
            wx.hideToast();
            wx.showModal({
              title: '错误提示',
              content: '上传图片失败',
              showCancel: false,
              success: function (res) { }
            })
          }
        });
      }
    })
  },

  // 单击图片放大
  previewImage: function (e) {
    wx.previewImage({
      current: e.currentTarget.id, // 当前显示图片的http链接
      urls: this.data.files // 需要预览的图片http链接列表
    })
  },

  // 点击提交退款
  Refunds: function(){
    var that = this;
    var OrderId = that.data.Id;
    var Cause = that.data.Cause;
    var Explain = that.data.Explain;
    var Voucher = that.data.ServerFiles
    if (Cause == '请选择') {
      wx.showToast({
        title: '请选择退货/退款原因',
        icon: 'none',
        duration: 1500
      })
    } else {
      $.Ajax("api/refund", "POST", {
        orderId: OrderId,
        type: 0,
        cause: Cause,
        explain: Explain,
        voucher: Voucher
      }, function (res) {
        wx.showToast({
          title: res.m,
          icon: 'none',
          duration: 1500
        })
        setTimeout(function(){
          wx.navigateBack({
            delta: 1
          })
        },2000);
      })
    }
  }
})